package com.haizhi.util;

import org.apache.commons.codec.digest.DigestUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class GenerateMd5 {
    private static Logger logger = LoggerFactory.getLogger(GenerateMd5.class);

    public static String getHash(String filePath) throws IOException, NoSuchAlgorithmException {

        File file = new File(filePath);

        if (!file.exists()) {
            logger.error("文件不存在: {}", filePath);
            return "";
        }
        logger.info("当前开始计算MD5文件: {}", filePath);

        long startTime = System.currentTimeMillis();
        InputStream ins = new FileInputStream(file);

        byte[] buffer = new byte[1024 * 8];
        MessageDigest md5 = MessageDigest.getInstance("MD5");

        int len;
        while ((len = ins.read(buffer)) != -1) {
            md5.update(buffer, 0, len);
        }

        ins.close();
        String md5_result = DigestUtils.md5Hex(md5.digest());
        logger.info(" -------------------------------------------------------------------------------");
        logger.info("|当前文件名称: " + file.getName());
        logger.info("|当前文件大小: " + (file.length() / 1024 / 1024) + "MB");
        logger.info("|当前文件路径[绝对]: " + file.getAbsolutePath());
        logger.info("|MD5结果: " + md5_result);
        logger.info("|计算耗时: {} ms", (System.currentTimeMillis() - startTime));
        logger.info(" -------------------------------------------------------------------------------");
        return md5_result;
    }


    public static String getHash(Path filePath) throws IOException, NoSuchAlgorithmException {

        if (!Files.exists(filePath)) {
            logger.error("文件不存在: {}", filePath);
            return "";
        }

        logger.info("当前开始计算MD5文件: {}", filePath);

        long startTime = System.currentTimeMillis();
        InputStream ins = Files.newInputStream(filePath);

        byte[] buffer = new byte[1024 * 8];
        MessageDigest md5 = MessageDigest.getInstance("MD5");

        int len;
        while ((len = ins.read(buffer)) != -1) {
            md5.update(buffer, 0, len);
        }

        ins.close();
        String md5_result = DigestUtils.md5Hex(md5.digest());
        logger.info(" -------------------------------------------------------------------------------");
        logger.info("|当前文件名称:" + filePath.getFileName());
        logger.info("|当前文件大小:" + (Files.size(filePath) / 1024 / 1024) + "MB");
        logger.info("|当前文件路径[绝对]:" + filePath.toString());
        logger.info("|MD5结果:" + md5_result);
        logger.info("|计算耗时: {} ms", (System.currentTimeMillis() - startTime));
        logger.info(" -------------------------------------------------------------------------------");
        return md5_result;
    }

    public static void main(String[] args) {
        long start = System.currentTimeMillis();
        logger.info("开始计算文件MD5值,请稍后...");
        String filePath = "/tmp/download-dir/SPRING实战  第4版.pdf";
        String hash = null;
        try {
            hash = GenerateMd5.getHash(filePath);
        } catch (Exception e) {
            e.printStackTrace();
        }
        logger.info("MD5:" + hash);
        long end = System.currentTimeMillis();
        logger.info("一共耗时:" + (end - start) + "毫秒");

        start = System.currentTimeMillis();
        try {
            hash = GenerateMd5.getHash(Paths.get(filePath));
        } catch (Exception e) {
            e.printStackTrace();
        }
        logger.info("MD5:" + hash);
        end = System.currentTimeMillis();
        logger.info("一共耗时:" + (end - start) + "毫秒");
    }
}